﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgorithmCenter
{
    public class AlgorithmCenter200714
    {
        private char[,] a = new char[,]{ 
            {'w','.','.','.','.','.','.','.','.','w','w','.' },
            {'.','w','w','w','.','.','.','.','.','w','w','w' },
            {'.','.','.','.','w','w','.','.','.','w','w','.' },
            {'.','.','.','.','.','.','.','.','.','w','w','.' },
            {'.','.','.','.','.','.','.','.','.','w','.','.' },
            {'.','.','w','.','.','.','w','.','.','w','.','.' },
            {'.','w','.','w','.','.','.','.','.','w','w','.' },
            {'w','.','w','.','w','.','.','.','.','.','w','.' },
            {'.','w','.','w','.','.','.','.','.','.','w','.' },
            {'.','.','w','.','.','.','.','.','.','.','w','.'}
        };

        private int h => a.GetLength(0);//10
        private  int w => a.GetLength(1);//12


        /// <summary>
        /// 计算水洼个数
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public int CaluatePuddleCount()
        {
            
            int count = 0;
            //查找矩阵中的w，从'w'开始dfs
            for (int i=0;i<h;i++)
            {
                for (int j = 0; j < w; j++)
                {
                    
                    if (a[i,j] == 'w')
                    {
                        
                        //对每个'w'进行8个方向上的dfs
                        Dfs(i,j);

                        count++;
                    }
                }
            }
            
            return count;
        }

        private void Dfs(int x,int y)
        {
            a[x,y] = '.';

            for (int dx = -1; dx <= 1; dx++)
            {
                for (int dy = -1; dy <= 1; dy++)
                {
                    int nx = x + dx;
                    int ny = y + dy;
                 
                    if (nx >= 0 && nx < h && ny >= 0 && ny < w && a[nx, ny] == 'w')
                        Dfs(nx,ny);
                }
            }
        }


    }
}
